﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using SmartHotel.Dto;
using SmartHotel.VIPCard.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Yitter.IdGenerator;

namespace SmartHotel.VIPCard
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppVIPCardService : ApplicationService, ISmartHotelAppVIPCardService
    {
        private readonly IRepository<SmartHotel_VIPTable, string> repository;
        public SmartHotelAppVIPCardService(IRepository<SmartHotel_VIPTable, string> repository)
        {
            this.repository = repository;
        }
        public async Task<ResultDto<int>> DeleteVIPCard(string id)
        {
            await repository.DeleteAsync(id);
            return new ResultDto<int> { Code = Result.Success };
        }

        public async Task<ResultDto<List<GetVIPCardDto>>> GetAllVIPCard()
        {
            var list = await repository.GetListAsync();
            var dto = ObjectMapper.Map<List<SmartHotel_VIPTable>, List<GetVIPCardDto>>(list);
            return new ResultDto<List<GetVIPCardDto>> { Code = Result.Success, Data = dto };
        }

        public async Task<ResultDto<GetVIPCardDto>> GetByIdVIPCard(string id)
        {
            var list = await repository.GetAsync(x => x.Id == id);
            return new ResultDto<GetVIPCardDto> { Code = Result.Success, Data = list };
        }

        public async Task<ResultDto<CreateandUpdateVIPCard>> InsertVIPCard(CreateandUpdateVIPCard vipCard)
        {
            YitIdHelper.SetIdGenerator(new IdGeneratorOptions());
            var lid = YitIdHelper.NextId();
            var cid = lid.ToString();

            vipCard.Id = cid;
            await repository.InsertAsync(ObjectMapper.Map<CreateandUpdateVIPCard, SmartHotel_VIPTable>(vipCard));
            return new ResultDto<CreateandUpdateVIPCard> { Code = Result.Success };
        }

        public async Task<ResultDto<CreateandUpdateVIPCard>> UpdateVIPCard(CreateandUpdateVIPCard vipCard)
        {
            var data = await repository.FindAsync(vipCard.Id);
            var list = ObjectMapper.Map(vipCard, data);
            await repository.UpdateAsync(list);
            return new ResultDto<CreateandUpdateVIPCard> { Code = Result.Success };
        }

        public object PostVIPUpload(IFormFile file)
        {
            string path = System.IO.Directory.GetCurrentDirectory() + @"/wwwroot/VIP/";
            string filename = file.FileName;
            string pathname = path + filename;
            using (var srout = System.IO.File.Create(pathname))
            {
                file.CopyTo(srout);
                srout.Flush();
                srout.Close();
            }
            return filename;
        }

    }
}
